// map.js
// import QQMapWX from '../../../libs/qqmap-wx-jssdk.min.js'
import QQMapWX from '../../../utils/qqmap'
Page({
  data: {
    latitude: 40.060539,
    longitude: 116.343847,
    ids: '',//报修id
    repairItemId: '',//已审核通过的房屋id
    houseInfo: '',//房屋信息
    repairItemName: '',//报修项目名称
    mobile: '',//手机号
    appointment: '',//预约时间
    description: '',//报修描述
    status: '',//报修状态
    attachment: [],//附件
    markers: [ // 起点：传智播客
      {
        id: 1,
        latitude: 40.060539,
        longitude: 116.343847,
        width: 24,
        height: 30,
        iconPath: '/static/images/marker.png',
      },
      // 终点：云趣园1区
      {
        id: 2,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 40,
        height: 40,
      },],//标记点
  },
  onLoad({ id }) {
    this.repairDetail(id)
    // console.log('报修详情id', id);
  },
  //报修详情
  async repairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    //数据回显
    const { houseInfo, repairItemName, mobile, appointment, description, status, attachment } = res
    this.setData({
      houseInfo, repairItemName, mobile, appointment, description, status, attachment
    })
    this.setData({ ids: res.id })
    console.log('报修详情', res.id, res);

    //status为2 (上门中)时，显示修改按钮
    if (status === 2) {
      // 调用百度地图绘制路线
      this.createPl()
    }
  },

  //百度地图绘制路线
  createPl() {
    const _this = this
    QQMapWX.direction({
      mode: 'driving',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: '40.060539,116.343847', // 传智播客
      to: '40.086757,116.328634', // （示例）云趣园1区
      success: function (res) {
        console.log(res);
        let ret = res;
        let coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        let kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        _this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  //修改报修
  repairUpdate() {
    //跳转到报修表单页
    wx.navigateTo({
      url: `/repair_pkg/pages/form/index?id=${this.data.ids}`,
    })
  },
  //取消报修
  async repairCancel() {
    const res = await wx.http.put(`/cancel/repaire/${this.data.ids}`)
    console.log('取消报修', res);
    wx.navigateTo({
      url: '/repair_pkg/pages/list/index',
    })
  }

})
